package com.xinjian.jianba.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.xinjian.jianba.dao.dwmid.UserMediaDao;
import com.xinjian.jianba.dto.StreamInfos;
import com.xinjian.jianba.dto.TranscodeCompleteDto;
import com.xinjian.jianba.dto.VedioDto;
import com.xinjian.jianba.dto.VideoDto;
import com.xinjian.jianba.enity.UserMediaEnity;
import com.xinjian.jianba.exception.BusinessException;
import com.xinjian.jianba.exception.ResponseCode;
import com.xinjian.jianba.service.VideoService;
import com.xinjian.jianba.utils.AliVideoUtil;
import com.xinjian.jianba.utils.JsonUtil;
@Service
public class VideoServiceImpl implements VideoService{

	@Autowired
	private UserMediaDao userMediaDao;
	@Override
	public int saveVideoUrl(VedioDto vedioDto) {
		int result=0;
		Date date=new Date();
		UserMediaEnity userMediaEnity=new UserMediaEnity();
		userMediaEnity.setVideoId(vedioDto.getVideoId());
		userMediaEnity.setUid(Long.parseLong(vedioDto.getUserId()));
		List<UserMediaEnity> list=userMediaDao.select(userMediaEnity);
		if(list.size()>0){
			throw new BusinessException(ResponseCode.ERROR, "该视频已上传");
		}
		userMediaEnity.setUid(null);
		List<UserMediaEnity> listCheck=userMediaDao.select(userMediaEnity);
		if(listCheck.size()>0){
			userMediaEnity.setUid(Long.parseLong(vedioDto.getUserId()));
			userMediaEnity.setMtype("vod");
			userMediaEnity.setImgStr(vedioDto.getImgUrl()==null?"":vedioDto.getImgUrl());
			userMediaDao.updateVod(userMediaEnity);
		}else{
			try{
			userMediaEnity.setUid(Long.parseLong(vedioDto.getUserId()));
			userMediaEnity.setType(1);
			userMediaEnity.setVideoId(vedioDto.getVideoId());
			userMediaEnity.setAddress(vedioDto.getVideoId());
			userMediaEnity.setImgStr(vedioDto.getImgUrl()==null?"":vedioDto.getImgUrl());
			userMediaEnity.setMtype("vod");
			userMediaEnity.setCreTime(new Timestamp(date.getTime()));
			userMediaEnity.setTimeAdd(date);
			result=userMediaDao.insertVideo(userMediaEnity);
			}catch(DuplicateKeyException e){
				UserMediaEnity userMedia=new UserMediaEnity();
				userMedia.setUid(Long.parseLong(vedioDto.getUserId()));
				userMediaEnity.setMtype("vod");
				userMediaEnity.setImgStr(vedioDto.getImgUrl()==null?"":vedioDto.getImgUrl());
				result=userMediaDao.updateVod(userMediaEnity);
			}
		}
		return result;
	}
	@Override
	public int vodBack(String json) {
		TranscodeCompleteDto transcodeCompleteDto=JsonUtil.json2Obj(json, TranscodeCompleteDto.class);
		String EventType=transcodeCompleteDto.getEventType();
		StreamInfos ctreamInfos=null;
		int result=0;
		if("TranscodeComplete".equals(EventType)){
			String videoId=transcodeCompleteDto.getVideoId();
			String status=transcodeCompleteDto.getStatus();
			if("success".equals(status)){
				String statusChild=transcodeCompleteDto.getStreamInfos().get(0).getStatus();
				if("success".equals(statusChild)){
					VideoDto dto = AliVideoUtil.getVideoInfo(videoId);
					UserMediaEnity userMediaEnity=new UserMediaEnity();
					userMediaEnity.setVideoId(videoId);
					List<UserMediaEnity> list=userMediaDao.select(userMediaEnity);
					if(list.size()>0){
						userMediaEnity.setUid(null);
						userMediaEnity.setPlayUrl(dto.getPlayUrl());
						userMediaEnity.setMp4Url(dto.getMp4Url());
						result=userMediaDao.updateVod(userMediaEnity);
					}else{
						try{
						Date date=new Date();
						userMediaEnity.setPlayUrl(dto.getPlayUrl());
						userMediaEnity.setMp4Url(dto.getMp4Url());
						userMediaEnity.setType(1);
						userMediaEnity.setVideoId(videoId);
						userMediaEnity.setAddress(videoId);
//						userMediaEnity.setMtype("vod");
						userMediaEnity.setCreTime(new Timestamp(date.getTime()));
						userMediaEnity.setTimeAdd(date);
						result=userMediaDao.insertVodId(userMediaEnity);
						}catch(DuplicateKeyException e){
							UserMediaEnity userMedia=new UserMediaEnity();
							userMedia.setPlayUrl(dto.getPlayUrl());
							userMedia.setMp4Url(dto.getMp4Url());
							result=userMediaDao.updateVod(userMedia);
						}
					}
				}
			}
			
			
		}
		return result;
	}
	@Override
	public int deleteVod(String videoIds) {
		videoIds=videoIds.replaceAll("\\s*", "");//去除字符串中的所有空格
		if((videoIds.length()-1)==videoIds.lastIndexOf(",")){
			throw new BusinessException(ResponseCode.ERROR, "不能以,结尾");
		}
		if(videoIds.indexOf(",")==0){
			throw new BusinessException(ResponseCode.ERROR, "不能以,开始");
		}
		String[] videoId=videoIds.split(",");
		if(videoId.length>20){
			throw new BusinessException(ResponseCode.ERROR, "最多支持批量删除20个");
		}
		int result=userMediaDao.deleteVod(videoId);
		if(result==0){
			throw new BusinessException(ResponseCode.ERROR, "视频id不存在");
		}
		
		AliVideoUtil.deleteVideo(videoIds);
		return result;
	}

}
